<?php
/**
 * Comment.php
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */

/** Blog_Post */
require_once 'Post.php';

/** base class */
require_once 'Madoqua/Db/Table/Abstract.php';

/**
 * Blog_Comment
 *
 * comments for blog post
 *
 * @category   Madoqua
 * @package    Module
 * @subpackage Blog
 */
class Blog_Comment extends Madoqua_Db_Table_Abstract {

    /**
     * table name
     *
     * @var string
     */
    protected $_name = 'comment';

    
    /**
     * reference map
     *
     * @var array
     */
    protected $_referenceMap    = array(
        'Blog_Post' => array(
            'columns'           => 'post_id',
            'refTableClass'     => 'Blog_Post',
            'refColumns'        => 'id'
        )
    );

    /**
     * fetch a list of comments, complete with post information
     *
     * @param Zend_Db_Select $where
     * @return array
     */
    public function fetchList($where = null, $order = null, $count = 0, $offset = 0) {
        if ($where == null)
        {
            $where = $this->getAdapter()->select();
        }
        //set up select if none was given

        // TODO this is kjust a kludge for sqlite, query needs re-worked
        $orderCol = $this->_primary[1];
        if($this->getAdapter() instanceof Zend_Db_Adapter_Pdo_Sqlite) {
            $orderCol = 'id';
        }
        
        if ($order == null)
        {
            $order = 'c.' . $orderCol . ' DESC';
        }
        $where->order($order);

        if ($count != 0)
        {
            $where->limit($count, $offset);
        }

        $postModel = new Blog_Post();
        $postInfo = $postModel->info();
        $fetchCols = array();
        foreach($postInfo['cols'] as $col) {
            $fetchCols['post_' . $col] = 'p.' . $col;
        }
        //all cols from the post model, prefixed with post_

        $where->from(array('c' => $this->_name))
        ->join(array('p' => $this->_prefix . 'post'), 'c.post_id = p.id', $fetchCols);
        //set up the select

        
        $result = $this->getAdapter()->fetchAll($where);
        foreach($result as $key => $item) {
            $result[$key] = (object) $item;
        }
        //sqlite bind bug.. .wtf?
        
        return $result;
    }

    /**
     * fetch a list of comments by post
     *
     * @param Post $post
     * @return array
     */
    public function fetchByPost(Zend_Db_Table_Row $post)
    {
        return $this->fetchAll($this->select()->where('post_id = ?', $post->id)
                                              ->where('accepted = 1'));
    }
    
    /**
     * delete comments by post id
     *
     * @param int $postId Id of the post for which comments should be deleted
     * @return int The number of comments deleted
     */
	public function deleteByPostId($postId) 
	{
	    if(!$postId) {
	        throw new Exception('$postId must be specified');
	    }
		return $this->delete('post_id = ' . (int)$postId);
	}
	
	/**
	 * Accept a comment
	 * 
	 * Will set a comment's accepted flag to 1
	 *
	 * @param int $id The comment ID
	 * @return Blog_Comment Provides a fluent interface
	 */
	public function accept($id) {
	    $id = (int)$id;
        if(!$id) {
	        throw new Exception('$id must be specified');
	    }
	    $this->update(array('accepted' => 1), 'id = ' . $id);
	    return $this;
	}
	
	/**
	 * Reject a comment
	 * 
	 * Will set a comment's accepted flag to 0
	 *
	 * @param int $id The comment ID
	 * @return Blog_Comment Provides a fluent interface
	 */
	public function reject($id) {
	    $id = (int)$id;
        if(!$id) {
	        throw new Exception('$id must be specified');
	    }
	    $this->update(array('accepted' => 0), 'id = ' . $id);
	}
	
	/**
	 * Mark a comment as spam
	 * 
	 * Will set a comment's spam flag to 1
	 *
	 * @param int $id The spam ID
	 * @return Blog_Comment Provides a fluent interface
	 */
	public function markAsSpam($id) {
	    $id = (int)$id;
        if(!$id) {
	        throw new Exception('$id must be specified');
	    }
	    $this->update(array('spam' => 1), 'id = ' . $id);
	}
	
	/**
	 * Mark a comment as 'not spam'
	 * 
	 * Will set a comment's spam flag to 0
	 *
	 * @param int $id The comment ID
	 * @return Blog_Comment Provides a fluent interface
	 */
	public function markAsHam($id) {
	    $id = (int)$id;
        if(!$id) {
	        throw new Exception('$id must be specified');
	    }
	    $this->update(array('spam' => 0), 'id = ' . $id);
	}
}